package com.sir.tao.carhire.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.ContractEntity;
import com.sir.tao.carhire.service.ContractService;
import com.sir.tao.carhire.util.PageUtil;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dyy
 * @data 2021/12/24 22:34
 * @description:
 */
@Api(tags = "合同管理接口")
@RequestMapping("/contract")
@RestController
public class ContractController {

    @Resource
    private ContractService contractService;


    /**
     * 新增一份合同
     *
     * @param contractEntity 合同实体
     * @return 是否成功
     */
    @PostMapping("/add")
    @ApiOperation("新增一份合同")
    @ApiImplicitParam(name = "新增一份合同", value = "新增一份合同")
    public CommonResult<?> addContract(@RequestBody ContractEntity contractEntity) {
        boolean i = contractService.save(contractEntity);
        if (i) {
            return CommonResult.success();
        } else {
            return CommonResult.fail("新增合同失败");
        }
    }

    /**
     * 修改一份合同
     *
     * @param contractEntity 合同实体
     * @return 是否成功
     */
    @PostMapping("/put")
    @ApiOperation("修改一份合同")
    @ApiImplicitParam(name = "修改一份合同", value = "修改一份合同")
    public CommonResult<?> putContract(@RequestBody ContractEntity contractEntity) {
        boolean i = contractService.updateById(contractEntity);
        if (i) {
            return CommonResult.success();
        } else {
            return CommonResult.fail("修改合同失败");
        }
    }

    /**
     * 条件分页查询合同列表
     * @return 分页结果
     */
    @GetMapping("/list")
    @ApiOperation("条件分页查询合同列表")
    @ApiImplicitParam(name = "条件分页查询合同列表", value = "条件分页查询合同列表")
    public CommonResult<?> getAllContract(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                          String search){
        if (pageNo <= 0){
            return CommonResult.fail("页码不能为负数");
        }
        List<ContractEntity> list = contractService.list(new LambdaQueryWrapper<ContractEntity>()
                .like(!StringUtils.isEmpty(search),ContractEntity::getFirstParty,search)
                .or()
                .like(!StringUtils.isEmpty(search),ContractEntity::getPartyB,search)
        ).stream().sorted(Comparator.comparing(ContractEntity::getSignTime)).collect(Collectors.toList());
        PageUtil page = PageUtil.doPage(pageNo,pageSize, list);
        return CommonResult.success(page);
    }

    /**
     * 根据id作废一份合同
     * @param contractId 合同id
     * @return 是否删除成功
     */
    @DeleteMapping("/delete/{contractId}")
    @ApiOperation("根据id作废一份合同")
    @ApiImplicitParam(name = "根据id作废一份合同", value = "根据id作废一份合同")
    public CommonResult<?> deleteContractById(@PathVariable String contractId){
        LambdaUpdateWrapper<ContractEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ContractEntity::getId,contractId)
                .set(ContractEntity::getStatus,3);
        boolean i = contractService.update(lambdaUpdateWrapper);
        if (i) {
            return CommonResult.success();
        } else {
            return CommonResult.fail("废除合同失败");
        }
    }

    /**
     * 合同审核
     * @param contractId 合同id
     * @return 合同审核
     */
    @GetMapping("/examine/{contractId}")
    @ApiOperation("合同审核")
    @ApiImplicitParam(name = "合同审核", value = "合同审核")
    public CommonResult<?> examineContractById(@PathVariable String contractId){
        LambdaUpdateWrapper<ContractEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper
                .eq(ContractEntity::getId,contractId)
                .set(ContractEntity::getIsExamine,1)
                .set(ContractEntity::getStatus,2);
        boolean i = contractService.update(lambdaUpdateWrapper);
        if (i) {
            return CommonResult.success();
        } else {
            return CommonResult.fail("合同审核失败");
        }
    }

    /**
     * 查看租凭合同
     * @param contractId 合同id
     * @return 合同审核
     */
    @GetMapping("/get/{contractId}")
    @ApiOperation("查看租凭合同")
    @ApiImplicitParam(name = "查看租凭合同", value = "查看租凭合同")
    public CommonResult<?> getContractById(@PathVariable String contractId){
        ContractEntity contractEntity = contractService.getById(contractId);
        return CommonResult.success(contractEntity);
    }

}